@section('content')
<div class="am-g m-cart">
	<div class="am-u-md-12">
		<div class="m-cart-hd am-cf">
			<h1>购物车</h1>
			<span class="free-tip">订单满@config('mao.free_postage_limit')元免运费</span>
		</div>
	</div>
	<div class="am-u-md-12 m-cart-table-box">
		<div class="m-cart-loading"><span class='am-icon-spinner am-icon-spin'></span></div>
	</div>
	
	<div class="am-u-md-12 ">
		<div class="m-cart-back">
			<a href="{{URL::to('/product-list')}}"><i class="am-icon-angle-double-left"></i> 继续购物</a>
		</div>
		<div class="m-cart-stat">
			<div>小计:<span class="subtal-value"></span></div>
			<div class="freight">运费:<span class="freight-value"></span></div>
			<div class="total">总计:<span class="total-value"></span></div>
		</div>
	</div>
	
	
    <div class="am-u-md-12 ">
    	<div class="m-cart-pay-btn-box">
    	<form action="{{URL::to('order-form')}}" method="get" id="order-from">
    	{{Form::token()}}
    	<button type="button" class="m-btn submit" id="submit-cart">立即结算</button>
    	</form>
    	</div>
    </div>
    
    @if(!empty($recommends))
    <div class="am-u-md-12 m-text-xs">
	<div class="m-recommends">
		<p class="am-margin-top am-margin-bottom">您可能喜欢以下商品</p>
		<ul class="am-avg-md-4 am-thumbnails am-cf">
		  @foreach($recommends as $p)
		  <li>
		  	  <a href="{{URL::to('product',$p->id)}}" title="{{$p->name}}">
		      <img src="{{Images::url($p->image,200,240)}}" alt="{{$p->name}}"/>
		      <div>
		        <div class="brand">{{$p->brand}}</div>
		        <div>{{$p->name}}</div>
		      </div>
		    </a>
		  </li>
		@endforeach
		</ul>
	</div>
	</div>
	@endif
</div>
@stop

@section('script')
<script type="text/x-handlebars-template" id="cart-tpl">
<table class="am-table m-cart-table">
    <thead>
        <tr>
            <th>商品</th>
            <th>价格</th>
            <th class="quantity-td">数量</th>
			<th>金额</th>
			<th>删除</th>
        </tr>
    </thead>
    <tbody>
		@{{#each this}}
        <tr data-id="@{{id}}" data-size_id="@{{size_id}}" data-color_id="@{{color_id}}" data-quantity="@{{quantity}}" data-price="@{{price}}">
            <td>
				<div class='m-cart-item am-cf'>
					<a href="@{{url 'product/' id}}"><img src='@{{imgurl image 72 72}}'></img><a>
			  		<div class='m-cart-item-bd'>
			  			<div class='name'>@{{enname}}</div>
			  			<div class='name'>@{{name}}</div>
						@{{#if size}}
                        <div class='name'>尺寸:@{{size}}</div>
						@{{/if}}
						@{{#if color}}
                        <div class='name'>颜色:@{{color}}</div>
						@{{/if}}
			  		</div>
				</div>
			</td>
            <td>¥@{{fnumber price}}</td>
            <td class="quantity-td"><button class='m-btn m-btn-mini quantity-btn decrement-btn'>&minus;</button><span class='quantity'>@{{quantity}}</span><button class='m-btn m-btn-mini quantity-btn increment-btn'>+</button></td>
			<td class="money-td">¥<span class='money'>@{{multi-fnumber price quantity}}<span></td>
			<td><span class='am-close delete-cart-product close-btn'>&times;</span></td>
        </tr>
		@{{/each}}
    </tbody>
</table>
</script>
<script type="text/javascript">
$(function(){
	var tpl = $("#cart-tpl").text();
	var template = Handlebars.compile(tpl);
	var products = $.cart.list();
	$('.m-cart-table-box').html(template(products));

	

	function refreshCartStat(products){
		var subtotal = 0;
		var freeFreight = false;
		$.each(products,function(i,e){
			subtotal += e.quantity * e.price;
			if (e.postage_mode == 2){
				freeFreight = true;
			}
		});

		$(".subtal-value").text("¥ "+$.maoutil.formatNumber(subtotal));
		if (subtotal >= $.config.freePostageLimit){
			$(".freight-value").text("免运费");
			$(".total-value").text("¥"+$.maoutil.formatNumber(subtotal));
		} else {
			if (freeFreight){
				$(".freight-value").text("免运费");
			} else {
				$(".freight-value").text("¥"+$.maoutil.formatNumber($.config.postage));
			}
			$(".total-value").text("¥"+$.maoutil.formatNumber((subtotal+$.config.postage)));
		}
	}

	function updateQuantity($tr,diff){
		var p = $tr.data();
		var quantity = p['quantity'] + diff;
		if (quantity <= 0){
			return;
		}

		$tr.data('quantity',quantity);
		$tr.find(".quantity").text(quantity);
		$tr.find(".money").text(quantity * p['price']);

		var products;
		if (diff > 0){
			products = $.cart.increment(p);
		} else {
			products = $.cart.decrement(p);
		}
		refreshCartStat(products);
		//var product = $.cart.get(p);
	}
	//刷新统计数据
	refreshCartStat(products);
	$(document).on("click",".decrement-btn",function(e){
		updateQuantity($(this).closest("tr"),-1);
	});

	$(document).on("click",".increment-btn",function(e){
		updateQuantity($(this).closest("tr"),1);
	});

	$(document).on("click",".delete-cart-product",function(e){
		var $tr = $(this).closest("tr");
		var product = $tr.data();
		var products = $.cart.destroy(product);
		$tr.remove();
		refreshCartStat(products);
		
	});

	$("#submit-cart").click(function(){
		var list = $.cart.list();
		if (list.length == 0){
			var msgTpl = "<div class='m-favorite-success'><span>您的购物车里没有商品，请您选择购买的商品!</span><a href='{{URL::to('product-list')}}'><span class='m-go-favorite'>查看商品列表</span></a></div>";
			$.boxer($(msgTpl));
		}

		if (!$.config.login){
			$.user.login();
		}

		//检查库存是否足够
		$("#submit-cart").button("loading");
		$.post("{{URL::to('order/check-quantity')}}",function(data){
			if (data.success){
				$("#order-from").submit();
			} else {
				if (data.error_no == "1" || data.error_no == 1){
					var errors = data.errors;
					$.each(errors,function(i,e){
						var $tr;
						if (e.size_id){
							$tr = $(".m-cart-table").find("tr[data-id="+e.product_id+"][data-size_id="+e.size_id+"]");
						} else if (e.color_id){
							$tr = $(".m-cart-table").find("tr[data-id="+e.product_id+"][data-color_id="+e.color_id+"]");
						} else {
							$tr = $(".m-cart-table").find("tr[data-id="+e.product_id+"]");
							if ($tr.length > 1){
								$tr.each(function(i,e){
									var size_id = $(this).data('size_id');
									var color_id = $(this).data('color_id');
									if (!size_id && !color_id){
										$tr = $(this);
										return false;
									}
								});
							}
							//$tr = $(".m-cart-table").find("tr[data-id="+e.product_id+"][data-color_id=''][data-size_id='']");
						}
						$tr.find(".quantity-td").find(".current_quantity").remove();
						$tr.find(".quantity-td").append("<div class='current-quantity'>当前库存量"+e.current_quantity+"</div>");
					});
				}
			}
			$("#submit-cart").button("reset");
		});
	});
	
});
</script>
@stop
